//
//  Helper2048.m
//  2048_Demo
//
//  Created by SongLian on 16/8/7.
//  Copyright © 2016年 SongLian. All rights reserved.
//

#import "Helper2048.h"

static const NSInteger kTotalSection = 4;
static const NSInteger kTotalRow = 4;
static const NSTimeInterval kAnimationTimeInterval = 0.3;

@interface Helper2048 ()



@end

@implementation Helper2048

- (instancetype)init {
    if (self = [super init]) {

        _set = [NSMutableSet set];
    }
    return self;
}


- (NSArray *)obtainArrayBySection:(NSInteger)section {
    NSMutableArray *sectionArray = SLMutableArray;
    for (SLLabel *label in self.set) {
        if (label.indexPath.section == section) {
            [sectionArray addObject:label];
        }
    }
    
    [sectionArray sortUsingComparator:^NSComparisonResult(SLLabel * _Nonnull obj1, SLLabel * _Nonnull obj2) {
        if (obj1.indexPath.row < obj2.indexPath.row) {
            return NSOrderedAscending;
        }
        return NSOrderedDescending;
    }];
    return sectionArray;
}


- (NSArray *)obtainArrayByRow:(NSInteger)row {
    NSMutableArray *rowArray = SLMutableArray;
    for (SLLabel *label in self.set) {
        if (label.indexPath.row == row) {
            [rowArray addObject:label];
        }
    }
    
    [rowArray sortUsingComparator:^NSComparisonResult(SLLabel * _Nonnull obj1, SLLabel * _Nonnull obj2) {
        if (obj1.indexPath.section < obj2.indexPath.section) {
            return NSOrderedAscending;
        }
        return NSOrderedDescending;
    }];
    return rowArray;
}

- (SLLabel *)obtainLabelWithIndexPath:(NSIndexPath *)indexPath {
    for (SLLabel *label in self.set) {
        if ([indexPath isEqual:label.indexPath]) {
            return label;
        }
    }
    return nil;
}

- (void)addLabel:(SLLabel *)label {
    if (self.set.count < kTotalRow * kTotalSection) {
        [self.set addObject:label];
    }
}
- (void)removeLabel:(SLLabel *)label {
    [self.set removeObject:label];
}

- (SLLabel *)createLabel {
    if (self.set.count < kTotalRow * kTotalSection) {
        NSIndexPath *indexPath = [self obtainIndexPath];
        if ([self isHasIndexPath:indexPath]) {
            return [self createLabel];
        } else {
            SLLabel *label = [[SLLabel alloc] initWithFrame:CGRectMake(indexPath.row*(70+12)+12, indexPath.section*(70+12)+12, 70, 70)];
            label.backgroundColor = SLColor_RGB(238, 228, 218);
            label.text = [self obtain_2_or_4];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:40];
            label.indexPath = indexPath;
            [self.set addObject:label];
            return label;
        }
    }
    return nil;
}

- (NSIndexPath *)obtainIndexPath {
    NSInteger location = arc4random()%16;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:location%4 inSection:location/4];
    return indexPath;
}

- (BOOL)isHasIndexPath:(NSIndexPath *)indexPath {
    for (SLLabel *label in self.set) {
        if ([label.indexPath isEqual:indexPath]) {
            return YES;
        }
    }
    return NO;
}

- (NSString *)obtain_2_or_4 {
    NSArray *array = @[@"2",@"4"];
    NSInteger random = arc4random()%2;
    return array[random];
}


- (void)moveLabelWithDirection:(UISwipeGestureRecognizerDirection)direction {
    
    __block NSInteger count = 0;
    
    NSEnumerationOptions option;
    if (direction == UISwipeGestureRecognizerDirectionUp || direction == UISwipeGestureRecognizerDirectionLeft) {
        option = NSEnumerationConcurrent;
    } else {
        option = NSEnumerationReverse;
    }
    
    if (direction == UISwipeGestureRecognizerDirectionUp || direction == UISwipeGestureRecognizerDirectionDown) {
        for (NSInteger i = 0; i < kTotalRow; i++) {
            NSArray *rowArray = [self obtainArrayByRow:i];
            
            NSInteger testInteger;
            
            if (option == NSEnumerationConcurrent) {
                testInteger = [self zhengxuCompareArray:rowArray];
            } else {
                testInteger = [self lixuCompareArray:rowArray];
            }
            
            if (testInteger == -1) {
                [rowArray enumerateObjectsUsingBlock:^(SLLabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
                        
                        if (option == NSEnumerationConcurrent) {
                            label.sl_y = idx*(70+12)+12;
                            label.indexPath = [NSIndexPath indexPathForRow:i inSection:idx];
                        } else {
                            label.sl_y = (kTotalSection - rowArray.count + idx)*(70+12)+12;
                            label.indexPath = [NSIndexPath indexPathForRow:i inSection:(kTotalSection - rowArray.count + idx)];
                        }
                    } completion:^(BOOL finished) {
                        count++;
                        if (count == self.set.count) {
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"addLabel" object:nil];
                        }
                    }];
                }];
            } else {
                [rowArray enumerateObjectsUsingBlock:^(SLLabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
                        if (option == NSEnumerationConcurrent) {
                            
                            if (idx >= testInteger + 1) {
                                label.sl_y = (idx-1)*(70+12)+12;
                                label.indexPath = [NSIndexPath indexPathForRow:i inSection:idx-1];
                            }
//                                else {
//                                label.sl_y = idx*(70+12)+12;
//                                label.indexPath = [NSIndexPath indexPathForRow:i inSection:idx];
//                            }
                        } else {
                            
                            if (idx <= testInteger - 1) {
                                label.sl_y = (kTotalSection - rowArray.count + idx+1)*(70+12)+12;
                                label.indexPath = [NSIndexPath indexPathForRow:i inSection:(kTotalSection - rowArray.count + idx+1)];
                            }
//                            else {
//                                label.sl_y = (kTotalSection - rowArray.count + idx)*(70+12)+12;
//                                label.indexPath = [NSIndexPath indexPathForRow:i inSection:(kTotalSection - rowArray.count + idx)];
//                            }
                        }
                    } completion:^(BOOL finished) {
                        
                        if (count == self.set.count) {
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"addLabel" object:nil];
                        }
                        
                        if (option == NSEnumerationConcurrent) {
                            if (idx == testInteger) {
                                [label removeFromSuperview];
                                [self.set removeObject:label];
                            }
                            
                            if (idx == testInteger +1) {
                                label.text = @(label.text.integerValue*2).stringValue;
                            }
                        } else {
                            if (idx == testInteger) {
                                [label removeFromSuperview];
                                [self.set removeObject:label];
                                
                            }
                            
                            if (idx == testInteger - 1) {
                                label.text = @(label.text.integerValue*2).stringValue;
                            }
                            
                        }
                        
                        
                        count++;
                        
                    }];
                }];
            }
            
            
            
            
            
            
        }
    } else {
        
        
        for (NSInteger i = 0; i < kTotalSection; i++) {
            
            NSArray *sectionArray = [self obtainArrayBySection:i];
            
            NSInteger testInteger;
            if (option == NSEnumerationConcurrent) {
                testInteger = [self zhengxuCompareArray:sectionArray];
            } else {
                testInteger = [self lixuCompareArray:sectionArray];
            }
            
            if (testInteger == -1) {
                
                
                [sectionArray enumerateObjectsUsingBlock:^(SLLabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    
                    
                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
                        if (option == NSEnumerationConcurrent) {
                            
                            label.sl_x = idx*(70+12)+12;
                            label.indexPath = [NSIndexPath indexPathForRow:idx inSection:i];
                        } else {
                            label.sl_x = (kTotalRow - sectionArray.count + idx)*(70+12)+12;
                            label.indexPath = [NSIndexPath indexPathForRow:(kTotalRow - sectionArray.count + idx) inSection:i];
                        }
                        
                        
                        
                        
                    } completion:^(BOOL finished) {
                        count++;
                        if (count == self.set.count) {
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"addLabel" object:nil];
                        }
                    }];
                }];
            } else {
                [sectionArray enumerateObjectsUsingBlock:^(SLLabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    
                    
                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
                        if (option == NSEnumerationConcurrent) {
                            
                            
                            if (idx >= testInteger + 1) {
                                label.sl_x = (idx-1)*(70+12)+12;
                                label.indexPath = [NSIndexPath indexPathForRow:idx-1 inSection:i];
                            }
//                            else {
//                                label.sl_x = idx*(70+12)+12;
//                                label.indexPath = [NSIndexPath indexPathForRow:idx inSection:i];
//                            }
                            
                            
                            
                            
                        } else {
                            
                            if (idx <= testInteger - 1) {
                                label.sl_x = (kTotalRow - sectionArray.count + idx+1)*(70+12)+12;
                                label.indexPath = [NSIndexPath indexPathForRow:(kTotalRow - sectionArray.count + idx+1) inSection:i];
                            }
//                            else {
//                                label.sl_x = (kTotalRow - sectionArray.count + idx)*(70+12)+12;
//                                label.indexPath = [NSIndexPath indexPathForRow:(kTotalRow - sectionArray.count + idx) inSection:i];
//                            }
                            
                            
                            
                        }
                        
                        
                        
                        
                    } completion:^(BOOL finished) {
                        
                        if (count == self.set.count) {
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"addLabel" object:nil];
                        }
                        
                        
                        
                        
                        
                        
                        if (option == NSEnumerationConcurrent) {
                            if (idx == testInteger) {
                                [label removeFromSuperview];
                                [self.set removeObject:label];
                            }
                            
                            if (idx == testInteger +1) {
                                label.text = @(label.text.integerValue*2).stringValue;
                            }
                        } else {
                            if (idx == testInteger) {
                                [label removeFromSuperview];
                                [self.set removeObject:label];
                                
                            }
                            
                            if (idx == testInteger - 1) {
                                label.text = @(label.text.integerValue*2).stringValue;
                            }
                        }
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        count++;
                        
                        
                        
                        
                    }];
                }];
            }
        }
    }
    
}


- (NSInteger)zhengxuCompareArray:(NSArray<SLLabel *> *)array {
    if (array.count >1) {
        for (NSInteger i = 0; i < array.count - 1; i++) {
            if ([array[i].text isEqualToString:array[i+1].text]) {
                return i;
            }
        }
    }
    return -1;
}

- (NSInteger)lixuCompareArray:(NSArray<SLLabel *> *)array {
    if (array.count >1) {
        for (NSInteger i = array.count-1; i >0 ; i--) {
            if ([array[i].text isEqualToString:array[i-1].text]) {
                return i;
            }
        }
    }
    return -1;
}
@end











//[rowArray enumerateObjectsUsingBlock:^(SLLabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
//    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
//        if (option == NSEnumerationConcurrent) {
//            label.sl_y = idx*(70+12)+12;
//            //                        label.frame = CGRectMake(label.sl_x, idx*(70+12)+12, 70, 70);
//            label.indexPath = [NSIndexPath indexPathForRow:i inSection:idx];
//        } else {
//            label.sl_y = (kTotalSection - rowArray.count + idx)*(70+12)+12;
//            //                        label.frame = CGRectMake(label.sl_x, (kTotalSection - rowArray.count + idx)*(70+12)+12, 70, 70);
//            label.indexPath = [NSIndexPath indexPathForRow:i inSection:(kTotalSection - rowArray.count + idx)];
//        }
//    }];
//}];
//
//
//
//
//}
//} else {
//    for (NSInteger i = 0; i < kTotalSection; i++) {
//        NSArray *sectionArray = [self obtainArrayBySection:i];
//        
//        [sectionArray enumerateObjectsUsingBlock:^(SLLabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
//            [UIView animateWithDuration:kAnimationTimeInterval animations:^{
//                if (option == NSEnumerationConcurrent) {
//                    label.sl_x = idx*(70+12)+12;
//                    //                        label.frame = CGRectMake(idx*(70+12)+12, label.sl_y, 70, 70);
//                    label.indexPath = [NSIndexPath indexPathForRow:idx inSection:i];
//                } else {
//                    label.sl_x = (kTotalRow - sectionArray.count + idx)*(70+12)+12;
//                    //                        label.frame = CGRectMake((kTotalRow - sectionArray.count + idx)*(70+12)+12, label.sl_y, 70, 70);
//                    label.indexPath = [NSIndexPath indexPathForRow:(kTotalRow - sectionArray.count + idx) inSection:i];
//                }
//            }];













